package website.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import website.advice.BaseResponse;
import website.domain.Article;
import website.domain.ResModel;
import website.dto.ArticleDTO;
import website.service.ArticleService;

import java.util.List;

@Api("博客文章控制类")
@Slf4j
@BaseResponse
@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation("查询文章详情")
    @GetMapping("/{id}")
    public ResModel<Article> getById(@PathVariable Integer id){
        return articleService.getArticle(id);
    }

    @ApiOperation("分页分类查询文章列表")
    @GetMapping("/{current}/{size}/{cid}")
    public ResModel<Page<Article>> getList(@PathVariable Integer current, @PathVariable Integer size,@PathVariable Integer cid, ArticleDTO articleDTO){
        return articleService.getList(current,size,cid,articleDTO);
    }

    @ApiOperation("分页查询用户文章列表")
    @GetMapping("/user/{current}/{size}/{uid}")
    public ResModel<Page<Article>> getListByUid(@PathVariable Integer current, @PathVariable Integer size,@PathVariable Integer uid){
        return articleService.getListByUid(current,size,uid);
    }

    @ApiOperation("查询推荐文章")
    @GetMapping("/recommend/{cid}/{id}")
    public ResModel<List<Article>> getRecommend(@PathVariable Integer cid,@PathVariable Integer id){
        return articleService.getRecommend(cid,id);
    }

    @ApiOperation("查询热门文章")
    @GetMapping("/hot")
    public ResModel<List<Article>> getHotArticle(){
        return articleService.getHotArticle();
    }

    @ApiOperation("更新浏览量")
    @GetMapping("/views/{id}")
    public ResModel addViews(@PathVariable Integer id){
        return articleService.addViews(id);
    }

    @ApiOperation("更新点赞量")
    @GetMapping("/likes/{id}/{uid}")
    public ResModel likes(@PathVariable Integer id,@PathVariable Integer uid){
        return articleService.likes(id,uid);
    }

    @ApiOperation("发布文章")
    @PostMapping(path = "",produces = "application/json")
    public ResModel issueArticle(@RequestBody Article article){
        return articleService.issueArticle(article);
    }

    @ApiOperation("编辑文章")
    @PutMapping(path = "",produces = "application/json")
    public ResModel updateArticle(@RequestBody Article article){
        return articleService.updateArticle(article);
    }
}
